package com.gxa.modules.service.lx.impl;

import com.gxa.common.constant.PageInfo;
import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.lx.GoodsMapper;

import com.gxa.modules.pojo.GoodsType;
import com.gxa.modules.service.lx.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: zls
 * Date: 2021/12/28 08:54:19
 * Description:
 * Version: V1.0
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired(required = false)
    private GoodsMapper goodsMapper  ;

    @Override
    public R2 queryGoodsList(Map map) {

        if(map.get("nowPage") == null && map.get("pageSize") == null){
            map.put("nowPage",PageInfo.NOWPAGE);
            map.put("pageSize",PageInfo.PAGESIZE);
        }
        Integer nowPage = ((Integer) map.get("nowPage")-1)*(Integer)map.get("pageSize");
        map.put("nowPage",nowPage);
        Integer sumPage = goodsMapper.sumPage(map);
        List<Map> Goodslist= goodsMapper.queryGoodsList(map);
        List list = new ArrayList();
        list.add(sumPage);
        list.add(Goodslist);
        R2 r2 = new R2();
        if(list != null){
            r2.setCode(200);
            r2.setMsg("查询成功");
            r2.setData(list);
        }else{
            r2.setCode(500);
            r2.setMsg("系统开小差了");
        }
        return r2;
    }



    @Override
    public R2 intoShop(Map map) {
        Integer result = 0;
        R2 r2 = new R2();
        Map map1 = new HashMap();
        List<Integer> goodsIdList = (List)map.get("goodsIdList");
       for(Integer item : goodsIdList){
           map1 = goodsMapper.queryExit((Integer)map.get("shopId"),item);
           if(map1 != null){
               r2.setCode(500);
               r2.setMsg("已有该商品");
               return r2;
           }
       }
        for(Integer item : goodsIdList){
            result = goodsMapper.intoShop((Integer)map.get("shopId"),item);
        }
        if(result != 0){
            r2.setCode(200);
            r2.setMsg("入库成功");
        }
        return r2;
    }
}
